package org.vy.dal.dao;

import java.io.ByteArrayInputStream;
import java.util.List;

import org.vy.dal.QueryManager;

public class VyPrintLabelReportInstanceDAO {
	private static final String FIND_MAX_REPORT_ID = "SELECT MAX(REPORT_INSTANCE_ID) as REPORT_INSTANCE_ID,null as REPORT_ID,null as START_TIME,null as STATUS,null as REPORT_DATA,null as END_TIME FROM <TABLE>";
	private static final String FIND_BY_REPORT_ID = "SELECT REPORT_INSTANCE_ID,START_TIME,STATUS,END_TIME,REPORT_ID FROM <TABLE> WHERE REPORT_ID = m_ReportId";
	private static final String FIND_DATA_BY_REPORT_INSTANCE_ID = "SELECT * FROM <TABLE> WHERE REPORT_INSTANCE_ID = ?";
	private static final String UPDATE_STATUS_DATE = "UPDATE <TABLE> SET STATUS = m_Status, END_TIME=m_EndTime  WHERE REPORT_INSTANCE_ID = m_ReportInstanceId";
	private static final String UPDATE_STATUS = "UPDATE <TABLE> SET STATUS = m_Status WHERE REPORT_INSTANCE_ID = m_ReportInstanceId";
	private static final String UPDATE_DATA = "UPDATE <TABLE> SET REPORT_DATA = ? WHERE REPORT_INSTANCE_ID = m_ReportInstanceId";
	private static final String INSERT = "INSERT INTO <TABLE> (REPORT_INSTANCE_ID,REPORT_ID,STATUS,START_TIME) VALUES (m_ReportInstanceId,m_ReportId,m_Status,m_StartTime);";
	private static final String DELETE = "DELETE FROM <TABLE> WHERE REPORT_INSTANCE_ID = m_ReportInstanceId";

	@SuppressWarnings("unchecked")
	public static List<VyPrintLabelReportInstanceDO> findByReportId(int id)
			throws Exception {
		VyPrintLabelReportInstanceDO dto = new VyPrintLabelReportInstanceDO();
		dto.setReportId(id);
		return (List<VyPrintLabelReportInstanceDO>) QueryManager
				.executeListQuery(dto, FIND_BY_REPORT_ID);
	}

	public static VyPrintLabelReportInstanceDO findDataByReportInstanceId(int instId)
			throws Exception {
		VyPrintLabelReportInstanceDO dto = new VyPrintLabelReportInstanceDO();
		Object[] params = new Object[1];
		params[0] = new Integer(instId);
		dto = (VyPrintLabelReportInstanceDO) QueryManager.executePreparedQuery(dto, FIND_DATA_BY_REPORT_INSTANCE_ID, params);
		return dto;
	}

	public static VyPrintLabelReportInstanceDO insert(
			VyPrintLabelReportInstanceDO dto) throws Exception {
		dto = (VyPrintLabelReportInstanceDO) QueryManager.executeInsert(dto,
				INSERT);
		dto = (VyPrintLabelReportInstanceDO) QueryManager.executeQuery(dto,
				FIND_MAX_REPORT_ID);
		return dto;
	}

	public static int updateStatus(
			VyPrintLabelReportInstanceDO dto) throws Exception {
		return QueryManager.executeUpdate(dto, UPDATE_STATUS);
	}

	public static int updateStatusEndTime(
			VyPrintLabelReportInstanceDO dto) throws Exception {
		return QueryManager.executeUpdate(dto, UPDATE_STATUS_DATE);
	}

	public static int updateData(
			Integer reportId, ByteArrayInputStream data) throws Exception {
		VyPrintLabelReportInstanceDO dto = new VyPrintLabelReportInstanceDO();
		dto.setReportInstanceId(reportId);
		Object[] params = new Object[1];
		params[0] = data;
		return QueryManager.executePreparedUpdate(dto, UPDATE_DATA, params);
	}

	public static boolean delete(VyPrintLabelReportInstanceDO dto)
			throws Exception {
		boolean i = QueryManager.executeDelete(dto, DELETE);
		return i;
	}
}
